package com.sunseen.intelligencebus.riding

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.ViewDataBinding
import com.sunseen.intelligencebus.R
import com.sunseen.intelligencebus.base.AbsBindingFragment
import com.sunseen.intelligencebus.common.QRCodeUtil
import com.sunseen.intelligencebus.databinding.FragmentQrCodeBinding
import com.sunseen.intelligencebus.user.BalanceGotEvent
import com.sunseen.intelligencebus.user.BalanceManager
import com.sunseen.intelligencebus.user.TopUpFragment
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class QRCodeFragment : AbsBindingFragment() {

    companion object {
        const val RequestTopUp = 0
        const val FromRequestTopUp = 1
        const val TopUpStr = "TopUp"
    }

    override fun setLayout(): Int = R.layout.fragment_qr_code

    var stationChar = "A"

    lateinit var balanceTv: TextView
    lateinit var qrcodeLayout: ConstraintLayout
    lateinit var qrcodeImg: ImageView

    override fun onBindView(viewDataBinding: ViewDataBinding, bundle: Bundle?) {
        EventBus.getDefault().register(this)

        stationChar = (arguments?.getString(RidingFragment.StationStr, "A")) ?: "A"
        val binding = viewDataBinding as FragmentQrCodeBinding

        balanceTv = binding.balanceValueTv
        qrcodeLayout = binding.qrCodeImageLayout
        qrcodeImg = binding.qrCodeImageView
        binding.hideCardTv.setOnClickListener {
            pop()
        }

        binding.topUpNowBtn.setOnClickListener {
            val topUpFragment = TopUpFragment()
            val topUpBundle = Bundle()
            topUpBundle.putInt(TopUpStr, FromRequestTopUp)
            topUpFragment.arguments = topUpBundle
            startForResult(topUpFragment, RequestTopUp)
        }

        BalanceManager.requestBalance()
    }

    override fun onDestroyView() {
        EventBus.getDefault().unregister(this)
        super.onDestroyView()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGotBalance(event: BalanceGotEvent) {
        val gotBalance: Double = event.balance
        balanceTv.text = gotBalance.toString()
        refreshQrcode(gotBalance)
    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        if (requestCode == RequestTopUp) {
            if (data != null) {
                val newBalance = data.getDouble(TopUpFragment.BalanceUpdatedStr)
                balanceTv.text = newBalance.toString()
                refreshQrcode(newBalance)
            }
        }
    }


    private fun refreshQrcode(balanceValue: Double) {
        if (balanceValue >= 5.0) {
            qrcodeImg.setImageBitmap(QRCodeUtil.createQRCodeBitmap(stationChar, 150, 150))
            qrcodeLayout.visibility = View.VISIBLE
            EventBus.getDefault().post(QrCodeRefershEvent(getStationId(stationChar)))
        } else {
            qrcodeLayout.visibility = View.GONE
        }
    }

    private fun getStationId(str: String): Int {
        when (str) {
            "A" -> return 0
            "B" -> return 1
            "C" -> return 2
            "D" -> return 3
            "E" -> return 4
            "F" -> return 5
            else -> return -1
        }
    }
}